package aca
import org.apache.http.HttpException
import org.openid4java.*
import org.openid4java.association.*
import org.openid4java.consumer.*
import org.openid4java.discovery.*
import org.openid4java.message.*
import org.openid4java.message.ax.FetchRequest
import org.openid4java.message.ax.FetchResponse
import org.openid4java.message.ax.AxMessage
import javax.swing.JOptionPane;
import java.nio.file.AccessDeniedException
import java.util.Map

class UserController {
	private static List<String> professores = new ArrayList<String>();
	private static List<String> administradores = new ArrayList<String>();
	private static List<String> dominios = new ArrayList<String>();
	static boolean ehProfessor = false
	static boolean estaLogado = false

	def index() {
		if (session.openidParams == null){
		redirect(uri:'/aca/user/login')
	}
		else{
			estaLogado = true
			String email = session.openidParams.ax.get("email").toString()
			String nome = session.openidParams.ax.get("firstName").toString()
			String sobrenome = session.openidParams.ax.get("lastName").toString()
			if (professores.contains(trataParametro(email))){
				ehProfessor = true
				redirect(controller: "professor", action: "index")
			}else if(administradores.contains(trataParametro(email))){
				redirect(controller: "user", action: "loginAdmin")
			}else{
				redirect(controller: "aluno", action: "index")
			}
		}
	}
	
	
	def index = {
		if (session.openidParams == null){
			redirect(action:"login")
		}
			else{
				estaLogado = true
				String email = session.openidParams.ax.get("email").toString()
				String nome = session.openidParams.ax.get("firstName").toString()
				String sobrenome = session.openidParams.ax.get("lastName").toString()
				if (professores.contains(trataParametro(email))){
					ehProfessor = true
					redirect(controller: "professor", action: "index")
				}else if(administradores.contains(trataParametro(email))){
				redirect(controller: "user", action: "loginAdmin")
				}else{
					redirect(controller: "aluno", action: "index")
				}
			}
		}
	
	def login = {
		for (int i=1; i <= Professor.count(); i++){
			professores.add(Professor.get(i).getEmail().toString())
		}
		for (int i=1; i <= Administrador.count(); i++){
			administradores.add(Administrador.get(i).getEmail().toString())
		}
		for (int i=1; i <= Dominio.count(); i++){
			dominios.add(Dominio.get(i).getDominio())
		}
	}

	def loggedin= {
	}

	def test = {
	}
	def loginSuccessful() {
		
		if (!dominios.contains(trataDominio(session.openidParams.ax.get("email").toString()))){
			redirect(action: "loggedout")
		}else{
		
		estaLogado = true
		String email = session.openidParams.ax.get("email").toString()
		String nome = session.openidParams.ax.get("firstName").toString()
		String sobrenome = session.openidParams.ax.get("lastName").toString()
		if (professores.contains(trataParametro(email))){
			ehProfessor = true
			redirect(controller: "professor", action: "index")
		}else if(administradores.contains(trataParametro(email))){
			redirect(action:"loginAdmin")
		}else{
			addAluno(trataParametro(email), trataParametro(nome) + " " + trataParametro(sobrenome))
			redirect(controller: "aluno", action: "index")
		}
		}
	}
	
	def loggedout={
		redirect(url:"https://www.google.com/accounts/Logout")
	}
	
	def loginAdmin={
		
	}
	
	def proibido = {
		[exception: new AccessDeniedException("Nobody allowed this web site, except for me!, Warning....")]
		
	}

	private String extraiDados(Map mapa){
		return "Email " + mapa.get("email").toString() + " Nome " + mapa.get("firstName").toString() + " Sobrenome " + mapa.get("lastName").toString()
	}

	private String trataParametro(String parametro){
		return parametro.substring(1, parametro.length()-1)
	}
	
	private String trataDominio( String email){
		return email.substring(email.indexOf("@") + 1, email.length() - 1)
	}

	private void addAluno(String email, String nome){
		def aluno = new Aluno(email: email, nome: nome)
		def alunoList = Aluno.list()
		if (! alunoList.contains(aluno)){
			aluno.save(failOnError: true)
		}
	}
	
}
